package ui.fragment.group_management;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.trek2000.android.enterprise.Enterprise;
import com.trek2000.android.enterprise.R;

import controller.company.SelectCompanyAsync;
import controller.group_management.GroupManagementAsync;
import controller.group_management.SettingTabInGroupFeedFragmentAsync;
import model.group_management.SettingTabInGroupFeedItem;
import ui.activity.group_management.GroupFeed;
import ui.activity.login.Login;
import utils.Utils;

public class SettingTabInGroupFeedFragment extends Fragment
        implements View.OnClickListener, View.OnTouchListener{
    /**
     * View section
     */
    public static EditText mEtGroupDescriptionInSettingTabInGroupFeedFragment;
    public static EditText mEtGroupNameInSettingTabInGroupFeedFragment;
    public static LinearLayout mLLSettingTabInGroupFeedFragment;
    // the others session
    public static Context mContext;

    public static Fragment newInstance() {

        SettingTabInGroupFeedFragment fragment = new SettingTabInGroupFeedFragment();
        return fragment;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = getLayoutInflater(savedInstanceState).inflate(
                R.layout.fragment_setting_tab_in_group_feed_fragment, container, false);
        // Initial views
        mContext = getActivity();
        initialViews(v);
        initialData();
        setDataForSettingTabInGroupFeedFragment();
//        disableSettingTabInGroupFeedFragment();
        return v;
    }

    @Override
    public boolean onTouch(View v, MotionEvent motionEvent) {
        switch (v.getId()) {
            case R.id.ll_setting_tab_in_group_feed_fragment:
//                enableSettingTabInGroupFeedFragment();
                hideSoftKeyboardInSettingTabInGroupFeedFragment();
                break;
        }
        return false;
    }

    /**
     * Initialize methods
     */
    private void initialData() {
        mLLSettingTabInGroupFeedFragment.setOnTouchListener(this);
    }

    private void initialViews(View v) {
        mEtGroupDescriptionInSettingTabInGroupFeedFragment = (EditText) v.findViewById(
                R.id.et_group_description_in_setting_tab_in_group_feed_fragment
        );
        mEtGroupNameInSettingTabInGroupFeedFragment = (EditText) v.findViewById(
                R.id.et_group_name_in_setting_tab_in_group_feed_fragment
        );
        mLLSettingTabInGroupFeedFragment= (LinearLayout) v.findViewById(
          R.id.ll_setting_tab_in_group_feed_fragment
        );
    }

    /**
     * The others methods
     */
    private void setDataForSettingTabInGroupFeedFragment() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
            new SettingTabInGroupFeedFragmentAsync(getActivity()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
                    String.valueOf(Login.user.getUserInfo().getUserGlobalId()),
                    String.valueOf(Login.user.getUserInfo().getAccessToken()),
                    String.valueOf(
                            Enterprise.company.getCompaniesOfUserInfoSelected().getUserCompanyId()),
                    String.valueOf(
                            Enterprise.company.getCompaniesOfUserInfoSelected().getCompanyId()),
                    String.valueOf(
                            Enterprise.groupManagement.getGroupIdSelected()),
                    "get_group_setting");
        else
            new SettingTabInGroupFeedFragmentAsync(getActivity()).execute(
                    String.valueOf(Login.user.getUserInfo().getUserGlobalId()),
                    String.valueOf(Login.user.getUserInfo().getAccessToken()),
                    String.valueOf(
                            Enterprise.company.getCompaniesOfUserInfoSelected().getUserCompanyId()),
                    String.valueOf(
                            Enterprise.company.getCompaniesOfUserInfoSelected().getCompanyId()),
                    String.valueOf(
                            Enterprise.groupManagement.getGroupIdSelected()),
                    "get_group_setting");
    }

    public static void displayDataToViewSettingTabInGroupFeedFragment(SettingTabInGroupFeedItem settingTabInGroupFeedItem) {
        mEtGroupDescriptionInSettingTabInGroupFeedFragment.setText(settingTabInGroupFeedItem.getGroupDescription());
        mEtGroupNameInSettingTabInGroupFeedFragment.setText(settingTabInGroupFeedItem.getGroupName());
    }

    private void disableSettingTabInGroupFeedFragment() {
        mEtGroupDescriptionInSettingTabInGroupFeedFragment.setEnabled(false);
        mEtGroupNameInSettingTabInGroupFeedFragment.setEnabled(false);
    }

    private void enableSettingTabInGroupFeedFragment() {
        mEtGroupDescriptionInSettingTabInGroupFeedFragment.setEnabled(true);
        mEtGroupNameInSettingTabInGroupFeedFragment.setEnabled(true);
    }

    public  static void hideSoftKeyboardInSettingTabInGroupFeedFragment() {
        Utils.hideSoftKeyboard(mContext, mEtGroupNameInSettingTabInGroupFeedFragment);
        Utils.hideSoftKeyboard(mContext, mEtGroupDescriptionInSettingTabInGroupFeedFragment);
    }
}
